<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%@include file="/static/common/header.jsp"%>
<%@include file="/static/common/easyui.jsp"%>
<%@include file="/static/common/common.jsp"%>
<link href="${ctx}/static/css/main.css" rel="stylesheet">
<script src="${ctx}/static/jquery/jquery.form.js" type="text/javascript"></script>

<script type="text/javascript">
	
	//获取叶子级别业务类型
	function changeSubBusiness() {
		var parent_business_id = $(":radio[name=business][checked]").val();
		document.getElementById('voice_sub_business').innerHTML="";
		$.ajax({
			type : 'post',
			url : '${ctx}/common/dic/dicLeaves?id='+parent_business_id,
			data : JSON.stringify({}),
			contentType : "application/json; charset=utf-8",
			dataType : 'json',
			beforeSend : function() {
				$("body").mask("正在处理中...");
			},
			success : function(data) {
				$("body").unmask();
				if (data && data.success == "true") {
					//重新加载当前页
					var json = JSON.parse(data.data);
					if(json.length>0) {
						var content = '';
						var top = '<div class="fl"><input type="radio" name="son_business" value=\"';
						var tail = '&nbsp;&nbsp;&nbsp;&nbsp;</div>';
						for(var i=0; i<json.length; i++) {
							content += top + json[i].id + '\"/>' + json[i].dicName + tail;
						}
						$('#voice_sub_business').append(content);
						$('#voice_sub_tr').removeClass("disn");
						
					}
				} else {
					$.messager.alert('提示', data.err_msg, 'info');
				}
			}
		});
	}

	function ExtractionBirthday(certNo, birth) {
		var certType = $('#certType').combobox('getValue');
		if (certType == "NI") {
			var txtparm = $('#certNo').val();
			if(txtparm.length == 18 || txtparm.length == 15) {
				if (checkCard(txtparm)) {
					var year = txtparm.substring(6, 10);
					var month = txtparm.substring(10, 12);
					var date = txtparm.substring(12, 14);
					$("#birth").datebox("setValue", year + "-" + month + "-" + date);
				} else {
					//$.messager.alert('提示', "身份证号码位数不正确", 'info');
					document.getElementByIdx_x(certNo).focus();
				}
			}
		}
	}
	
	//客户信息校验
	function saveData() {
		var custId = $("#custId").val();
		var flag = $("#custFlag").val();
		
		var custType = $(":radio[name=custType][checked]").val();
		var id = $('#id').val();
		var name = $('#name').val();
		var sex = $(":radio[name=sex][checked]").val();
		var mobile = $('#mobile').val();
		var acctNo = $("#acctNo").val();
		var certType = $("#certType").combobox("getValue");
		var certNo = $("#certNo").val();
		var birth = $("#birth").datebox("getValue");
		var telAbbr = $("#telAbbr").val();
		var telephone = $("#telephone").val();
		var faxAbbr = $("#faxAbbr").val();
		var fax = $("#fax").val();
		var dependency = $("#dependency").val();
		var province = $("#province").val();
		var city = $("#city").val();
		var email = $("#email").val();
		var address = $("#address").val();
		var zipCode = $("#zipCode").val();
		
		//客户类型
		if (custType == null || custType == '') {
			$.messager.alert('提示', '请选择客户类型', 'info');
			return false;
		}
		
		//工单字段校验
		var orderNo = $("#orderNo").val();
		var orderType = "1";
		var parentBusinessId = $(":radio[name=business][checked]").val();
		var subBusinessId = $(":radio[name=son_business][checked]").val();
		var dealFlag = $(":radio[name=dealFlag][checked]").val();
		var remark = $("#remark").val();
		if (parentBusinessId == null || parentBusinessId == '' || parentBusinessId == 'undefined') {
			$.messager.alert('提示', "请选择业务类型", 'info');
			return false;
		}
		if (subBusinessId == null || subBusinessId == '' || subBusinessId == 'undefined') {
			$.messager.alert('提示', "请选择详细业务类型", 'info');
			return false;
		}
		if (dealFlag == null || dealFlag == '' || dealFlag == 'undefined') {
			$.messager.alert('提示', "请选择是否处理", 'info');
			return false;
		}
		
		//消息推送校验
		var smsFlag = "N";
		var emailFlag = "N";
		if ($("#sms_flag").attr("checked")) {//选中  
			smsFlag = "Y";
		}
		if ($("#email_flag").attr("checked")) {//选中  
			emailFlag = "Y";
		}
		
		var mobileNo = $("#mobileNo").val();
		var emailAdd = $("#emailAdd").val();
		var smsContent = $("#smsContent").val();
		var emailContent = $("#emailContent").val();
		
		if(smsFlag=='Y') {
			if(validateMobile(mobileNo)) {
				if (smsContent == null || smsContent == '') {
					$.messager.alert('提示', "短信通知内容为空", 'info');
					return false;
				}
			} else {
				return false;
			}
		}
		
		if(emailFlag=='Y') {
			if(validateEmail(emailAdd)) {
				if (emailContent == null || emailContent == '') {
					$.messager.alert('提示', "邮件通知内容为空", 'info');
					return false;
				}
			} else {
				return false;
			}
		}
		
		//校验完成
		var data = {
			//客户信息
			custId: custId,
			name: name,
			sex: sex,
			mobile: mobile,
			acctNo: acctNo,
			certType: certType,
			certNo: certNo,
			birth: birth,
			telAbbr: telAbbr,
			telephone: telephone,
			faxAbbr: faxAbbr,
			fax: fax,
			dependency: dependency,
			province: province,
			city: city,
			email: email,
			address: address,
			zipCode: zipCode,
			custType: custType,
			//工单信息
			orderNo: orderNo,
			orderType: orderType,	
			recordId: $("#recordId").val(),
			businessId: subBusinessId,
			dealFlag: dealFlag,
			//status: 0,
			smsFlag: smsFlag,
			emailFlag: emailFlag,
			mobileNo: mobileNo,
			emailAdd: emailAdd,
			smsContent: smsContent,
			emailContent: emailContent,
			remark: remark,
			agentName : $("#agentName").val()
		};
		
		$.postJSON("${ctx}/infobird/call/saveOrder", data, function(data) {
			if (data && data.success == "true") {
				$.messager.alert('提示', '保存成功', 'info');
				$("#custFlag").val("Y");
				$("#custId").val(data.custId);
				$("#mobileNo").val(data.mobile);
				$("#emailAdd").val(data.email);
				initCallOrder();
			} else {
				$.messager.alert('提示', data.err_msg, 'info');
			}
		});
	}

	function clearData() {
		var radioes = document.getElementsByName("dealFlag");
		for ( var i = 0; i < radioes.length; i++) {
			if (radioes[i].checked) {
				radioes[i].checked = false;
				break;
			}
		}
		
		$('#voice_sub_tr').addClass("disn");
		$('#remark').val("");
		
		var list = document.getElementsByName("business");
		for ( var i = 0; i < list.length; i++) {
			if (list[i].checked) {
				list[i].checked = false;
				break;
			}
		}
	}

	function showNotice() {
		if ($("#sms_flag").attr("checked")) {//选中  
			$("#smsTd").css("display", "");
			$("#mobileTd").css("display", "");
			$("#mobileTd1").css("display", "none");
		} else {
			$("#smsTd").css("display", "none");
			$("#mobileTd").css("display", "none");
			$("#mobileTd1").css("display", "");
		}

		if ($("#email_flag").attr("checked")) {//选中  
			$("#emailTd").css("display", "");
			$("#contentTd").css("display", "");
			$("#contentTd1").css("display", "none");
		} else {
			$("#emailTd").css("display", "none");
			$("#contentTd").css("display", "none");
			$("#contentTd1").css("display", "");
		}
	}

	$(document).ready(
		function() {
			initCallOrder();
			document.title = $('#callNo').val();
	})
			
	function initCallOrder() {
		var custId = $("#custId").val();
		if(custId==null || custId=='') {
			custId = -1;
		}
		$('#callOrderTable').datagrid(
				{
					title : '<b>业务接触历史</b>',
					iconCls : 'icon-bumen',
					method : 'POST',
					url : '${ctx}/infobird/call/orderlist?custId='
							+ custId + '&orderType=1',
					nowrap: false,
					striped: true,
					collapsible:false,
					idField : 'id',
					columns : [ [ {
						field : 'orderType',
						title : '工单类型',
						align : 'center',
						width : 100,
						hidden : true,
						formatter : function(value) {
							if (value == "" || value == null) {
								return "";
							} else if (value == "1") {
								return "话务工单";
							} else if (value == "2") {
								return "结算工单";
							} else {
								return "";
							}
						}
					}, {
						field : 'orderNo',
						title : '工单编号',
						align : 'left',
						width : 120
					}, {
						field : 'businessFlow',
						title : '业务类型',
						align : 'left',
						width : 300
					}, {
						field : 'remark',
						title : '备注',
						align : 'left',
						width : 200
					}, {
						field : 'agentName',
						title : '坐席',
						width : 100
					}, {
						field : 'createTime',
						title : '录入时间',
						width : 100,
						formatter:formatterdate
					}, {
						field : 'dealTime',
						title : '处理时间',
						width : 100,
						formatter:formatterdate
					}, {
						field : 'dealFlag',
						title : '是否已处理',
						align : 'center',
						width : 100,
						formatter : function(value) {
							if (value == "" || value == null) {
								return "否";
							} else if (value == "Y") {
								return "是";
							} else if (value == "N") {
								return "否";
							}
						}
					}, {
						field : 'status',
						title : '处理状态',
						align : 'center',
						width : 100,
						hidden : true,
						formatter : function(value) {
							if (value == "" || value == null) {
								return "处理中";
							} else if (value == "0") {
								return "处理中";
							} else if (value == "1") {
								return "已退回";
							} else if (value == "2") {
								return "已处理";
							}
						}
					}, {
						field : 'id',
						title : '操作',
						align : 'center',
						width : 100,
						formatter : function(value, row, index) {
							return '<a href="#" class="viewcls" onClick="viewOrder(\''+row.id+'\', \'查看\')">[ 查看 ]</a>';
						}
					} ]],
					pagination : true,
					pageSize : 10,
					rownumbers : true,
					onLoadSuccess : function() {
						$('.viewcls').linkbutton({text:'查看',plain:true,iconCls:'icon-view'});
						$('#callOrderTable').datagrid('clearSelections'); //一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
					}
				});
	}
	
	function viewOrder(orderId) {
		var agentName = $('#agentName').val();
		var url = '${ctx}/infobird/call/viewHis?id=' + orderId + '&opt=VIEW' +
		'&stmp=' + (new Date()).getTime();
		var returnValue = window.showModalDialog(url, window,"dialogWidth=1200px;dialogHeight=500px");
		if(returnValue=='Y') {
			initCallOrder();
		}
	}

</script>

</head>

<body>

	<div id="J_Form">

		<form id="recordForm" name="recordForm" method="post">
			<!-- 隐藏域 -->
			<input type="hidden" id="custId" name="custId" value="${cust.id }">
			<input type="hidden" id="custFlag" name="custFlag" value="${custFlag}"> 
			<input type="hidden" id="custType" name="custType" value="${cust.custType}"> 
			<input type="hidden" id="recordId" name="recordId" value="${record.id}">
			<input type="hidden" id="callNo" name="callNo" value="${record.callNo}">
			<input type="hidden" id="newOrderNo" name="newOrderNo" value="${record.orderNo}">
			<input type="hidden" id="agentName" name="agentName" value="${agentName}">

			<!-- 客户类型选择 -->
			<table class="listTable">
				<tr>
					<th width="140px"><font color="red">&nbsp;&nbsp;*&nbsp;&nbsp;</font>客户类型：</th>
					<td>
						<div class="fl">
							<input type="radio" name="custType" value="1" 
								<c:if test="${cust.custType eq '1' }">checked="checked"</c:if> />
							服务机构&nbsp;&nbsp;&nbsp;&nbsp;
						</div>
						<div class="fl">
							<input type="radio" name="custType" value="2" 
								<c:if test="${cust.custType eq '2' }">checked="checked"</c:if> />
							已开户客户&nbsp;&nbsp;&nbsp;&nbsp;
						</div>
						<div class="fl">
							<input type="radio" name="custType" value="3" 
								<c:if test="${cust.custType eq '3' }">checked="checked"</c:if> />
							未开户客户&nbsp;&nbsp;&nbsp;&nbsp;
						</div>
						<div class="fl">
							<input type="radio" name="custType" value="4" 
								<c:if test="${cust.custType eq '4' }">checked="checked"</c:if> />
							批发商&nbsp;&nbsp;&nbsp;&nbsp;
						</div>
						<div class="fl">
							<input type="radio" name="custType" value="5" 
								<c:if test="${cust.custType eq '5' }">checked="checked"</c:if> />
							其他&nbsp;&nbsp;&nbsp;&nbsp;
						</div>
					</td>
				</tr>
			</table>

			<div style="height: 10px;"></div>

			<!-- 联系人信息 -->
			<table id="customerInfo" class="listTable" style="width: 100%">
				<caption>联系人信息</caption>
				<tbody>
					<tr>
						<th width="123px">姓名：</th>
						<td width="200px">
							<input type="text" id="name" name="name" value="${cust.name}" >
						</td>
						<th width="120px">性别：</th>
						<td width="200px">
							<input type="radio" name="sex" value="M" 
							<c:if test="${cust.sex eq 'M' }">checked="checked"</c:if> />
							男&nbsp;&nbsp;&nbsp;&nbsp; 
							<input type="radio" name="sex" value="F" 
							<c:if test="${cust.sex eq 'F' }">checked="checked"</c:if> />
							女
						</td>
						<th width="120px">手机号码：</th>
						<td width="200px">
							<c:if test="${empty cust.mobile && custFlag=='N' }">
								<input type="text" id="mobile" name="mobile" value="${cust.mobile}" />
							</c:if>
							<c:if test="${!empty cust.mobile || custFlag=='Y'}">
								<input type="text" id="mobile" name="mobile" value="${cust.mobile}" disabled="disabled" />
							</c:if>
						</td>
						<th width="120px">交易账号：</th>
						<td width="200px">
							<input type="text" id="acctNo" name="acctNo" value="${cust.acctNo}" >
						</td>
					</tr>
					<tr>
						<th width="120px">证件号码：</th>
						<td>
							<select name="certType" id="certType" class="easyui-combobox">
								<c:forEach items="${dicList}" var="dic">
									<option value="${dic.dicValue}"
										<c:if test="${cust.certType eq dic.dicValue }">selected="selected"</c:if>>${dic.dicName}</option>
								</c:forEach>
							</select>
						 	-
						 	<input type="text" id="certNo" name="certNo" value="${cust.certNo}"
								onchange="ExtractionBirthday('certNo', 'birth')"  />
						</td>
						<th width="120px">出生日期：</th>
						<td width="200px">
							<input id="birth" name="birth" value="${cust.birthDay}" class="easyui-datebox"
								style="width: 150px;" />
						</td>
						<th width="120px">固定电话：</th>
						<td>
							<input type="hidden" id="telAbbr" name="abbr" value="${cust.telAbbr}" style="width: 50px;" />
							<c:if test="${empty cust.telephone && custFlag=='N' }">
								<input type="text" id="telephone" name="telephone" value="${cust.telephone}" />
							</c:if>
							<c:if test="${!empty cust.telephone || custFlag=='Y'}">
								<input type="text" id="telephone" name="telephone" value="${cust.telephone}" disabled="disabled" />
							</c:if>
						</td>
						<th width="120px">传真号码：</th>
						<td>
							<input type="hidden" id="faxAbbr" name="faxAbbr" value="${cust.faxAbbr}" style="width: 50px;" />
							<input type="text" id="fax" name="fax" value="${cust.fax}" />
						</td>
					</tr>
					<tr>
						<th width="120px">号码归属地：</th>
						<td width="150px">
							<input type="hidden" id="province" name="province" value="${cust.province}">
							<input type="hidden" id="city" name="city" value="${cust.city}">
							<input type="text" id="dependency" name="dependency" value="${cust.dependency}" disabled="disabled">
						</td>
						<th width="120px">电子邮件：</th>
						<td width="150px">
							<input type="text" id="email" name="email" value="${cust.email}" >
						</td>
						<th width="120px">通讯地址：</th>
						<td>
							<input type="text" id="address" name="address" value="${cust.address}" >
						</td>

						<th width="120px">邮编：</th>
						<td>
							<input type="text" id="zipCode" name="zipCode" value="${cust.zipCode}" >
						</td>
					</tr>
				</tbody>
			</table>

			<div class="height10"></div>

			<table class="easyui-datagrid" id="callOrderTable"
				title="Basic DataGrid">
			</table>
			
			<div class="height10"></div>

			<!-- 话务信息 -->
			<table id="callInfo" class="listTable" style="width: 100%">
				<caption>话务信息</caption>
				<tbody>
					<tr>
						<th width="140px">工单编号：</th>
						<td>
							<input type="text" id="orderNo" name="orderNo" value="${record.orderNo}" disabled="disabled">
						</td>
					</tr>
					<tr>
						<th width="140px"><font color="red">&nbsp;&nbsp;*&nbsp;&nbsp;</font>业务类型：</th>
						<td>
							<c:if test="${!empty voiceBusiList}">
								<c:forEach items="${voiceBusiList}" var="voice">
									<div class="fl">
										<input type="radio" name="business" onclick="changeSubBusiness()" value="${voice.id}" 
										/>${voice.dicName}&nbsp;&nbsp;&nbsp;&nbsp;
									</div>
								</c:forEach>
							</c:if>
						</td>
					</tr>
					<tr id="voice_sub_tr" class="disn">
						<th width="140px"><font color="red">&nbsp;&nbsp;*&nbsp;&nbsp;</font>详细业务类型：</th>
						<td id="voice_sub_business">
						</td>
					</tr>
					<tr>
						<th width="140px">备注：</th>
						<td colspan="7"><textarea rows="3" cols="120" name="remark"
								id="remark" value="${order.remark}"></textarea></td>
					</tr>
					<tr>
						<th width="140px"><font color="red">&nbsp;&nbsp;*&nbsp;&nbsp;</font>是否已处理：</th>
						<td colspan="7">
							<input type="radio" name="dealFlag" value="Y"
							<c:if test="${order.dealFlag eq 'Y' }">checked="checked"</c:if> />
							是&nbsp;&nbsp;&nbsp;&nbsp; 
							<input type="radio" name="dealFlag" value="N"
							<c:if test="${order.dealFlag eq 'N' }">checked="checked"</c:if> />
							否
						</td>
					</tr>
				</tbody>
			</table>
			
				<div
					style="text-align: center; padding: 5px; margin-top: 5px; margin-bottom: 5px;">
					<a href="javascript:void(0);" id="btn-back" onclick="saveData();"
						class="easyui-linkbutton" iconCls="icon-save">保存</a>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:void(0);" id="btn-add" onclick="clearData();"
						class="easyui-linkbutton" iconCls="icon-reload">清空</a>
				</div>
			
			<div style="height: 10px;"></div>
			
			<!-- 短信&邮件通知 -->
			<table class="listTable" style="width: 100%">
				<tr>
					<th width="140px" height="50px" rowspan="2" >
						<input type="checkbox" id="sms_flag" name="sms_flag" onclick="showNotice();"/>
						短信通知：
					</th>
					<td id="mobileTd1" style="display: block" rowspan="2">
					</td>
					<td id="mobileTd" style="display: none">
						手机号码：
						<input type="text" id="mobileNo" name="mobileNo" value="${cust.mobile}">
					</td>
				</tr>
				<tr>
					<td id="smsTd" height="50px" style="display: none">
						<textarea rows="3" cols="120" name="smsContent" id="smsContent"></textarea>
					</td>
				</tr>
				<tr>
					<th width="140px" height="50px" rowspan="2">
						<input type="checkbox" id="email_flag" name="email_flag" onclick="showNotice();" />
						邮件通知：
					</th>
					<td id="contentTd1" style="display: block" rowspan="2">
					</td>
					<td id="contentTd" style="display: none">
						邮件地址：
						<input type="text" id="emailAdd" name="emailAdd" value="${cust.email}" >
					</td>
				</tr>
				<tr>
					<td id="emailTd" height="50px" style="display: none">
						<textarea rows="3" cols="120" name="emailContent" id="emailContent"></textarea>
					</td>
				</tr>
			</table>

		</form>

	</div>
	
    <div id="innerFramWindow" class="easyui-window" data-options="closed:true,modal:true,collapsible:false,minimizable:false,maximizable:false,draggable:true,onBeforeClose:function(){}" style="overflow: hidden">
  		<iframe id="innerWinFram" scrolling="yes" frameborder="0"  src='' style="width:100%;height:100%;"></iframe>
	</div>

</body>

</html>